<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include '../Module/Template_Copy_Module.php';
$Module=new Template_Copy_Module();
if(($_REQUEST["id"])){
    $id= $_REQUEST["id"];
    $item=$Module->getTemplate($id);
    genErorMSG($Module->error);
}
?>

    
    <div style="height: 40px;width: 100%;text-align: center">
        <h1><?=isset($id)?"Chỉnh Sửa : ":"Thêm mới"?><?=$item["name"]?></h1>
    </div>
    <div style="width: 100%">
        <table style="width:100%;" class="_form">
                    <tr>
                        <td style="">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txt_template_detail_name" placeholder="" value="<?=$item["name"]?>"/>
                                    <input type="hidden" id="txt_template_detail_id" placeholder="" value="<?=$item["id"]?>"/>
                                    <div class="label"><label>Name</label></div>
                                </div>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td>
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txt_template_detail_title" placeholder="" value="<?=$item["title"]?>"/>
                                    <div class="label"><label>Tiêu đề</label></div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txt_template_detail_image" placeholder="" value="<?=$item["image"]?>"/>
                                    <div class="label"><label>Hình ảnh</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txt_template_detail_image' );"
                                title="Chọn hình ảnh"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txt_template_detail_image1x" placeholder="" value="<?=$item["image1x"]?>"/>
                                    <div class="label"><label>Hình 1X</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txt_template_detail_image1x' );"
                                title="Chọn hình ảnh"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    
                                    <input type="text" id="txt_template_detail_image2x"  placeholder="" value="<?=$item["image2x"]?>"/>
                                    <div class="label"><label>Hình 2X</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right: 8px;cursor: pointer"
                                 onclick="BrowseServer( 'Images:/', 'txt_template_detail_image2x' );"
                                 title="Chọn hình ảnh"
                                 >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txt_template_detail_image3x" placeholder="" value="<?=$item["image3x"]?>"/>
                                    <div class="label"><label>Hình 3X</label></div>
                                </div>
                                <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                    onclick="BrowseServer( 'Images:/', 'txt_template_detail_image3x' );"
                                    title="Chọn hình ảnh"
                                    >
                                    <img src="./images/icon/16/folder_explore.png"/>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txt_template_detail_homepage" placeholder="" value="<?=$item["url"]?>"/>
                                    <div class="label"><label>Home Page</label></div>
                                </div>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td style="position: relative">
                            <div class="_input-label" type="select">
                            <div class="input" type="select">
                                <div class="select">
                                    <select id="cbx_template_detail_category">
                                        <option value="HTML5 Template">HTML5 Template</option>
                                        
                                    </select>
                                </div>
                                <div class="label"><label>Category</label></div>
                            </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <div class="input-full">
                                <div class="label"><label>Nội dung ngắn gọn</label></div>
                            </div>
                            <div style="padding:27px 10px 0 0;">
                                <textarea class="_s _w100" id="txt_template_detail_brief" rows="3" cols="103" 
                                            style="width: 100%;height: 100px;padding-left: 8px"

                                            ><?=$item["brief"]?></textarea>
                            </div>
                            
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">

                            
                            <div class="input-full">
                                <div class="label"><label>Nội dung</label></div>
                            </div>
                            <div style="padding:27px 0px 0 0;" id="editor_content">

                            </div>
                                <textarea class="_s _w100 mceEditor" id="txt_template_detail_des" rows="3" cols="103" 
                                            style="width: 100%;height: 200px"

                                            ><?=$item["description"]?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3" style="">
                            <span style="color: blue">Thêm lúc : <?=$item["insert"]?></span>
                            <?
                            $u=$item["update"];$d=$item["delete"];
                            if(isset($item["update"])){
                                echo "<br/><span style='color: orange'>cập nhật lúc : $u</span>";
                            }
                            if(isset($item["delete"])){
                                echo "<br/><span style='color: red'>Xóa lúc : $d</span>";
                            }
                            ?>

                        </td>
                    </tr>
                </table>
        <span id="warningmsg"></span>
        <div class="mybutton icon tooltip" 
            title="Tải Lại"
            onclick="$('.id-content-row.editting td[colspan=8]').load('Controller/?sUnit=05_template_detail_&id=<?=$id?>')"
            ><div><img src="./images/icon/media_floppy_green.png"/><span>Tải lại</span></div></div>
        <div class="mybutton icon tooltip<?=isset($item["delete"])?" disable":""?>" 
            title="Lưu"
            onclick="<?=isset($item["delete"])?"":(isset($id)?"update();":"save();")?>"
            ><div><img src="./images/icon/media_floppy_green.png"/><span>Lưu</span></div></div>
        <div class="mybutton icon tooltip" 
            onclick="huy()"
            title="Hủy"
            ><div><img src="./images/icon/addg.png"/><span>Hủy</span></div></div>

    </div>
    <script>
        $(document).ready(function(){
          $("#txt_template_detail_name").focus();
            tinyMCE.init({
                    mode : "textareas",
                    theme : "advanced",
                    content_css : "/Coffee/css/style.css",
                    editor_selector : "mceEditor",
                    editor_deselector : "mceNoEditor",
                    theme_advanced_resizing : true,
                    theme_advanced_resize_horizontal : false
            });
        });
        function huy(){
            $(".id-head-row.editting").removeClass("editting");
            $(".id-content-row.editting").removeClass("editting");
            $(".id-content-row").hide();
        }
        function save(){
            process("add");
        }
        function update(){
            
            process("edit");
        }
        function process(action){
            
            if(!_FcheckFilled($("#txt_template_detail_name").val())){$("#txt_template_detail_name").focus();return;}
            if(!_FcheckFilled($("#txt_template_detail_image").val())){$("#txt_template_detail_image").focus();return;}
            if(!_FcheckFilled($("#txt_template_detail_homepage").val())){$("#txt_template_detail_homepage").focus();return;}
            
            
          
            var surl="./Controller/";
            
            var sdata={
                    "controller":"TemplateController_",
                    "action":action,
                    "id":$("#txt_template_detail_id").val(),
                    "category":$("#cbx_template_detail_category").val(),
                    "name":$("#txt_template_detail_name").val(),
                    "image":$("#txt_template_detail_image").val(),
                    "image1":$("#txt_template_detail_image1x").val(),
                    "image2":$("#txt_template_detail_image2x").val(),
                    "image3":$("#txt_template_detail_image3x").val(),
                    "title":$("#txt_template_detail_title").val(),
                    "brief":$('#txt_template_detail_brief').val(),
                    "des":tinyMCE.get('txt_template_detail_des').getContent(),
                    "homepage":$("#txt_template_detail_homepage").val()
            };
            showmsg("Đợi tý. Đang xử lý.",null);
            jQuery.ajax({
                    type:"POST", 
                    data:sdata, 
                    dataType:"text", 
                    url:surl, 
                    success: function (data){
                        if(data=="true"){
                            $("#warningmsg").html("Thành Công.Tải lại trang trong 2 giây.");
                            $("#warningmsg").show();
                            setTimeout(function() {
                                $("#warningmsg").hide();
                                hidemsg();
                            },2000);
                            
                        }else{
                            hidemsg();
                            showerrorbox(null,data);
                            $("#warningmsg").html(data);
                            $("#warningmsg").show();
                        }

                    },
                    error: function (xhr, ajaxOptions, thrownError){
                            $("#warningmsg").html("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError);
                            $("#warningmsg").show();
                    }
            });

        }
    </script>
